package Search;

import java.util.Stack;

public class reverseList {
    public static  void main(String[] ages){
        System.out.println("Test");
        reverseList reverseList = new reverseList();
        ListNode head = new ListNode(1);
        ListNode start = head;
        for (int i=2;i<=5;i++){
            ListNode k = new ListNode(i);
            head.next = k;
            head = head.next;
        }
        start = reverseList.solution(start);


    }
    public ListNode solution(ListNode head){
        //1, 2, 3, 4, 5
        //2, 3, 4, 5  1
        //3, 4, 5  2  1

        //return  reverse(head, null);

        //创建两个节点 记录地址
        ListNode per = null;//前面节点
        ListNode curr = head;//head节点
        //1, 2, 3, 4
        // 第一轮
        //curr = 1;
        while(curr!=null){
            // next=2
            ListNode next = curr.next;
            // 1.next 指向 null
            curr.next = per;
            // per = 1
            per = curr;
            // curr =2;
            curr = next;
        }
        return per;





//        Stack<Integer> stack = new Stack();
//        ListNode start = head;
//        //空链表判断
//        if(start == null){
//            return head;
//        }
//        while(head.next != null){
//            stack.push(head.val);
//            head = head.next;
//        }
//        stack.push(head.val);
//        head = start;
//        while(!stack.empty()){
//            start.val = stack.pop();
//            start = start.next;
//        }
//        return head;
    }
    public ListNode reverse(ListNode head, ListNode temp){
        if (head == null){
            return temp;
        }
        ListNode start = reverse(head.next, head);
        head.next = temp;
        return  start;
    }

}

class ListNode{
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val){
        this.val= val;
    }
    ListNode(int val, ListNode next){
        this.val=val;
        this.next=next ;
    }
}

